package cn.flying.cloud.system.position.service.impl;

import javax.annotation.Resource;

import java.io.Serializable;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.flying.cloud.base.common.utils.Ft;
import cn.flying.cloud.base.common.vo.PageRequest;
import cn.flying.cloud.base.common.vo.PageResponse;
import cn.flying.cloud.common.enums.SeqTypeEnum;
import cn.flying.cloud.common.enums.StatusEnum;
import cn.flying.cloud.system.log.service.SysSeqInfoService;
import cn.flying.cloud.system.position.entity.SysPostInfo;
import cn.flying.cloud.system.position.entity.SysPostUser;
import cn.flying.cloud.system.position.mapper.SysPostInfoMapper;
import cn.flying.cloud.system.position.service.SysPostInfoService;
import cn.flying.cloud.system.position.service.SysPostUserService;
import cn.flying.cloud.system.user.entity.SysUserInfo;

/**
 * <p>
 * 岗位信息表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2024-04-02 13:06:48
 */
@Service
public class SysPostInfoServiceImpl extends ServiceImpl<SysPostInfoMapper, SysPostInfo> implements SysPostInfoService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private SysSeqInfoService sysSeqInfoService;
    @Resource
    private SysPostUserService sysPostUserService;

    /**
     * 分页查询岗位信息表数据
     *
     * @param request
     * @return
     */
    @Override
    public PageResponse<SysPostInfo> getPageList(PageRequest<SysPostInfo> request) {
        SysPostInfo param = request.getParam();
        LambdaQueryWrapper<SysPostInfo> wrapper = getLambdaQueryWrapper(param);
        Page<SysPostInfo> page = this.page(new Page<>(request.getPageNum(), request.getPageSize()), wrapper);
        return new PageResponse<>(page.getTotal(), (int) page.getCurrent(), page.getRecords());
    }

    /**
     * 构建查询对象
     *
     * @param param
     * @return
     */
    private LambdaQueryWrapper<SysPostInfo> getLambdaQueryWrapper(SysPostInfo param) {
        LambdaQueryWrapper<SysPostInfo> wrapper = new LambdaQueryWrapper<>();
        if (param != null) {
            if (Ft.String.isNotBlank(param.getPostId())) {
                wrapper.eq(SysPostInfo::getPostId, param.getPostId());
            }
            if (Ft.String.isNotBlank(param.getPostName())) {
                wrapper.like(SysPostInfo::getPostName, "%" + param.getPostName() + "%");
            }
            if (Ft.String.isNotBlank(param.getStatus())) {
                wrapper.eq(SysPostInfo::getStatus, param.getStatus());
            }
        }
        return wrapper;
    }

    /**
     * 列表查询岗位信息表数据
     *
     * @param sysPostInfo
     * @return
     */
    @Override
    public List<SysPostInfo> getList(SysPostInfo sysPostInfo) {
        LambdaQueryWrapper<SysPostInfo> wrapper = getLambdaQueryWrapper(sysPostInfo);
        return this.list(wrapper);
    }

    /**
     * 获取所有的岗位信息表数据
     *
     * @return
     */
    @Override
    public List<SysPostInfo> getAllPostList() {
        LambdaQueryWrapper<SysPostInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysPostInfo::getStatus, StatusEnum.STATUS_Y.getCode());
        return this.list(wrapper);
    }

    /**
     * 查询岗位信息表详情数据
     *
     * @param sysPostInfo
     * @return
     */
    @Override
    public SysPostInfo getOneObj(SysPostInfo sysPostInfo) {
        LambdaQueryWrapper<SysPostInfo> wrapper = getLambdaQueryWrapper(sysPostInfo);
        return this.getOne(wrapper);
    }


    /**
     * 新增岗位信息表数据
     *
     * @param sysPostInfo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean createObj(SysPostInfo sysPostInfo) {
        sysPostInfo.setPostId(sysSeqInfoService.getNextSeq(SeqTypeEnum.POST.name()));
        sysPostInfo.setTenantId(Ft.Local.TenantId());
        return this.save(sysPostInfo);
    }

    /**
     * 更新岗位信息表数据
     *
     * @param sysPostInfo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateObj(SysPostInfo sysPostInfo) {
        return this.saveOrUpdate(sysPostInfo);
    }

    /**
     * 删除岗位信息表数据
     *
     * @param sysPostInfo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteObj(SysPostInfo sysPostInfo) {
        LambdaQueryWrapper<SysPostInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysPostInfo::getPostId, sysPostInfo.getPostId());
        boolean removed = this.remove(wrapper);

        // 同时删除用户岗位信息
        LambdaQueryWrapper<SysPostUser> postWrapper = new LambdaQueryWrapper<>();
        postWrapper.eq(SysPostUser::getPostId, sysPostInfo.getPostId());
        sysPostUserService.remove(postWrapper);
        return removed;
    }

    /**
     * 根据Id删除
     *
     * @param id
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteById(Serializable id) {
        return this.removeById(id);
    }

    /**
     * 查询岗位对应的用户
     *
     * @param postId
     * @return
     */
    @Override
    public List<SysUserInfo> getUserByPostId(String postId) {
        return this.getBaseMapper().getUserByPostId(Ft.Local.TenantId(), postId);
    }
}
